<?php
include_once("dataprovider.php");

class HinhAnh
{
	var $MaHinhAnh;
	var $MaSanPham;
	var $Url;
	var $ThongTin;
	var $DaXoa=0;

	
	public static function LayHinhAnh_ChuaXoa_TheoMaHinhAnh($mahinh)
	{
		$sql="
			SELECT *
			FROM hinhanh
			WHERE DaXoa=0 AND MaHinhAnh=".$mahinh."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	public static function LayHinhAnh_DaXoa_TheoMaHinhAnh($mahinh)
	{
		$sql="
			SELECT *
			FROM hinhanh
			WHERE DaXoa=1 AND MaHinhAnh=".$mahinh."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	
	
	public static function LayDanhSachHinh_ChuaXoa_TheoMaSanPham($masp)
	{
		$sql = "
				SELECT *
				FROM hinhanh
				WHERE MaSanPham=".$masp." AND DaXoa=0
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsHinh[] = new HinhAnh;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$hinh = new HinhAnh;
			$hinh->MaHinhAnh = $row["MaHinhAnh"];
			$hinh->MaSanPham = $row["MaSanPham"];
			$hinh->Url = $row["Url"];
			$hinh->ThongTin = $row["ThongTin"];
			$hinh->DaXoa = $row["DaXoa"];
			$dsHinh[$i++] = $hinh;
		}
		return $dsHinh;
	}
	
	public static function LayDanhSachHinh_DaXoa_TheoMaSanPham($masp)
	{
		$sql = "
				SELECT *
				FROM hinhanh
				WHERE MaSanPham=".$masp." AND DaXoa=1
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsHinh[] = new HinhAnh;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$hinh = new HinhAnh;
			$hinh->MaHinhAnh = $row["MaHinhAnh"];
			$hinh->MaSanPham = $row["MaSanPham"];
			$hinh->Url = $row["Url"];
			$hinh->ThongTin = $row["ThongTin"];
			$hinh->DaXoa = $row["DaXoa"];
			$dsHinh[$i++] = $hinh;
		}
		return $dsHinh;
	}
	
	public static function LayDanhSachHinh_TatCa_TheoMaSanPham($masp)
	{
		$sql = "
				SELECT *
				FROM hinhanh
				WHERE MaSanPham=".$masp."
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsHinh[] = new HinhAnh;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$hinh = new HinhAnh;
			$hinh->MaHinhAnh = $row["MaHinhAnh"];
			$hinh->MaSanPham = $row["MaSanPham"];
			$hinh->Url = $row["Url"];
			$hinh->ThongTin = $row["ThongTin"];
			$hinh->DaXoa = $row["DaXoa"];
			$dsHinh[$i++] = $hinh;
		}
		return $dsHinh;
	}
	
	
	
	public static function ThemHinhAnh(HinhAnh $hinh)
	{
		$sql = "
			INSERT INTO hinhanh(MaSanPham,Url,ThongTin,DaXoa)
			VALUE (	".$hinh->MaSanPham.",'".$hinh->Url."','".$hinh->ThongTin."',".$hinh->DaXoa.")
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	public static function XoaHinhAnhTheoMa($mahinh)
	{
		$sql = "
			UPDATE hinhanh
			SET DaXoa=1
			WHERE MaHinhAnh=".$mahinh."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	public static function CapNhatHinhAnh(HinhAnh $hinh)
	{
		$sql = "
			UPDATE hinhanh
			SET MaSanPham=".$hinh->MaSanPham.",
				Url='".$hinh->Url."',
				ThongTin='".$hinh->ThongTin."',
				DaXoa=".$hinh->DaXoa."
			WHERE MaHinhAnh=".$hinh->MaHinhAnh."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	
	
	
}

?>